<template>
  <div class="data-form advertisement-form" v-loading.fullscreen.lock="loading">
    <el-form label-width="auto" size="medium">
      <el-form-item label="广告名称">
        <el-input v-if="type==='add'" v-model="advertisementForm.advertisementName" class="form-input">
        </el-input>
        <span v-else>{{advertisementForm.advertisementName}}</span>
      </el-form-item>
      <el-form-item label="广告类型">
        <el-select v-model="advertisementForm.advertisementType" class="form-input">
          <el-option v-for="item in typeList" :key="item.value" :value="item.value" :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="广告图片">
        <imgs-upload v-model="advertisementForm.advertisementUrl" :limit="1" :img-width="150" :img-height="150" :readonly="type==='look'">
        </imgs-upload>
      </el-form-item>
      <el-form-item label="跳转地址">
        <el-input v-model="advertisementForm.advertisementHref" class="form-input">
        </el-input>
        <div style="color:#F56C6C;font-size:14px;padding-top:4px;line-height:15px">
          跳转内部地址填写相对路径(不需要域名)，外部地址需要完整地址
        </div>
      </el-form-item>
      <el-form-item label="广告排序">
        <el-input-number v-model="advertisementForm.advertisementSort" class="form-input" :min="1" :controls="false" :precision="0">
        </el-input-number>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="advertisementForm.advertisementStatus" class="form-input">
          <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div style="text-align:center">
      <el-button size="medium" type="primary" @click="saveAdvertisement">
        <span v-if="type==='add'">新增</span>
        <span v-else>修改</span>
      </el-button>
    </div>
  </div>
</template>

<script>
  import AdvertisementForm from 'models/advertisement/advertisementForm';

  import ImgsUpload from 'commonComponents/imgsUpload';

  import AdvertisementApi from 'services/advertisementApi';

  export default {
    props: {
      advertisement: {
        type: Object,
        default () {
          return null
        }
      },
      type: {
        type: String,
        default: 'add'
      }
    },

    data() {
      let advertisementForm;
      if (this.type === 'add') {
        advertisementForm = new AdvertisementForm();
      } else {
        advertisementForm = _.cloneDeep(this.advertisement);
      }
      return {
        loading: false,
        advertisementForm,
        typeList: this.i18n.getDropDown('advertisement.advertisementSetting.type', [1, 2, 3]),
        statusList: this.i18n.getDropDown('advertisement.advertisementSetting.status', [1, 0])
      };
    },

    methods: {
      saveAdvertisement() {
        let advertisementForm = this.advertisementForm;
        let code = advertisementForm.checkValid();
        if (code !== '') {
          let msg = this.i18n.getMsg(`advertisement.advertisementSetting.error.${code}`);
          this.$message(msg);
          return;
        }

        if (this.type === 'add') {
          this.addAdvertisement(advertisementForm);
        } else {
          this.updateAdvertisement(advertisementForm);
        }
      },

      addAdvertisement(advertisementForm) {
        this.loading = true;
        AdvertisementApi.addAdvertisement(advertisementForm).then(() => {
          this.loading = false;
          this.$alert('广告新增成功', '信息', {
            callback: () => {
              this.$emit('save-success');
            }
          });
        }).catch(() => {
          this.loading = false;
        });
      },

      updateAdvertisement(advertisementForm) {
        this.loading = true;
        AdvertisementApi.updateAdvertisement(advertisementForm).then(() => {
          this.loading = false;
          this.$alert('广告修改成功', '信息', {
            callback: () => {
              this.$emit('save-success');
            }
          });
        }).catch(() => {
          this.loading = false;
        });
      }
    },

    components: {
      ImgsUpload
    }
  };

</script>

<style lang="scss" scoped>
  @import '~viewStyle/dataForm';

</style>
